home *** CD-ROM | disk | FTP | other *** search
/ The Programmer Disk / The Programmer Disk (Microforum).iso / xpro / tutor / pro10 / gwbt03.txt < prev    next >
Encoding:
Text File  |  1990-07-27  |  23.2 KB  |  480 lines

  1. Greetings!
  2.  
  3. As we re-convene for this month, there are several things that need to be 
  4. covered before we actually start the next lesson.
  5.  
  6. The first one that I want to cover is to clear up a mistake in the LOCATE.BAS 
  7. program in GWBT02.  I must confess that I finished writing that program 
  8. sometime around 2:00 am, and did not test it for errors as thoroughly as I 
  9. should have, otherwise I would have quickly noted a very large error.
  10.  
  11. As discussed in the message area, the use of the LOCATE statement is:
  12.  
  13.      LOCATE [row],[col]
  14.  
  15. where [row] is a number between 1 and 25, and represents the screen line that 
  16. you will print on; and [col] is a number between 1 and 80, and represents the 
  17. screen column you will print on.  Keeping this in mind, the LOCATE.BAS program 
  18. should have looked like this:
  19.  
  20. <please refer to enclosed file: LOCATE.BAS>
  21.  
  22. Second, I am going to start doing something a little different in the archived 
  23. version of the GWBT series - the archived version will separate the programs 
  24. and text into different files, all contained within the archive file.  The 
  25. text file will continue to have the programs inserted inside of the file.  
  26. Whichever one you wish to obtain, either the GWBTxx.TXT or GWBTxx.ZIP file, 
  27. the contents will be the same except that where the text file will contain the 
  28. program listing, the archived version will instead tell you which BASIC 
  29. program goes there.  This will save you from having to load a word processor 
  30. and 'cut' all the program files out of the text, or having to re-type all of 
  31. them in to try them out!
  32.  
  33. Finally, the consensus seems to be that most everyone would prefer to have the 
  34. live CONFERENCE session held on Friday or Saturday night, so this will be the 
  35. night that we will be doing that.  Watch the message area (IBMNEW, Message 
  36. Area 10) for the announcement on the first on-line conference.
  37.  
  38. ***************************************************************************
  39.      Homework Assignment #1 - Designing an INPUT screen
  40. ***************************************************************************
  41.  
  42. As you will remember, the first assignment was to take a pre-designed form (a 
  43. check, in this case) and design a way for the user to enter check information 
  44. for a business.
  45.  
  46. You might be asking why we're going to so much trouble as to print a 
  47. representation of the check on the screen and then jump around to fill in the 
  48. blanks.  Why not, instead, just write a simple routine that asks for each of 
  49. the needed pieces of information as questions?
  50.  
  51. The reason for the 'picture-and-fill-in-the-blanks' approach is that you may 
  52. not know WHO will be running programs you write.  It may be an experienced 
  53. computer user, or someone who just turned one on for the first time!
  54.  
  55. If it's the experienced user, they know what you're doing and will watch for 
  56. cues from your program on what to do next, so we don't worry too much about 
  57. them.
  58.  
  59. But, if it's the beginner...  They're usually scared out of their gourds that 
  60. they're going to press that 'magic key' that makes everything on the computer 
  61. go up in a cloud of smoke, or that they'll mess someone else's information up, 
  62. or that the computer 'just won't like them'!
  63.  
  64. So, for these users, we want to make them feel as much at home as we possibly 
  65. can.  Therefore, give them a blank check to look at, and they feel a little 
  66. more at home.  They've filled out hundreds of these little devils!  The only 
  67. difference now is that we want them to use a computer to fill in the blanks 
  68. instead of a pen.
  69.  
  70. The more 'at ease' you can make the user of your software feel, the more 
  71. likely he/she is to use it properly and not be so worried.
  72.  
  73. Before I present my solution to this problem, I want to stress AGAIN that 
  74. your program will probably not look a lot like mine in many respects.  This is 
  75. not a real problem!  As long as your program accomplishes the desired tasks 
  76. set out in the flowchart, minor differences will not make your solution any 
  77. worse than mine, only different.
  78.  
  79. <please refer to enclosed file: CHECK.BAS>
  80.  
  81. Let's examine the program one section at a time, so you can see what I was 
  82. thinking about as I wrote it, relating it to the 'flowchart' of steps we made 
  83. in the last lesson:
  84.  
  85. LINES 10 through 90 are simply comments that document (for the programmer) 
  86. what we're doing.  The variables used are listed by name and type, so you can 
  87. go back even years later and remember what you used each variable for.
  88.  
  89. LINE 100 marks the start of the first actual part of the program, is a clear 
  90. and concise reminder to you of what you're accomplishing here, in this case 
  91. setting up the screen and printing the check.
  92.  
  93. LINE 110 handles the first step of our flowchart:  Clear the screen of anything 
  94. already there.  
  95.  
  96. LINES 120 to 150 handle step two:  Move to line 25, print a message that the 
  97. user is entering a check, then return to line 1.
  98.  
  99. LINES 160 to 280 handle step three:  Print a reproduction of the check on the 
  100. screen, one line at a time.  This is probably where your program will start to 
  101. look markedly different from mine!
  102.  
  103. I prefer to use some sort of marker to show the user how much room they have 
  104. to enter information.  In this case, I used the underline (_) character to 
  105. represent the number of spaces available in each area to enter information (in 
  106. computereese, these are called 'input fields') so the user can see how much 
  107. room they have.
  108.  
  109. LINE 290 marks the start of the second phase of the program, getting the
  110. information from the user.
  111.  
  112. Now, a little thought went into the next few steps.  I wanted a way to show 
  113. the user where he/she was on the screen now, accept the input, then move on to 
  114. the next 'input field' and get more information.  I used the following 
  115. detailed flowchart to accomplish this:
  116.  
  117.   -----> Move the cursor to the next input field, then back two spaces
  118.   |                                    |
  119.   |                                    V
  120.   |              Use the INPUT command to get the information
  121.   |                                    |
  122.   |                                    V
  123.   |                Move back to the same INPUT point again
  124.   |                                    |
  125.   |                                    V
  126.   |          Print a single space to erase the question mark INPUT
  127.   |                      command put on the screen
  128.   |                                    |
  129.   |                                    V
  130.   <----------------<--------------------
  131.  
  132. Since INPUT prints a question mark followed by a blank space to indicate that 
  133. you are to supply some information, I simply added two blank spaces in front 
  134. of each 'input field' to make room for them, and subtracted two from the 
  135. actual start of the underlines.  BASIC's INPUT statement then printed its 
  136. question mark and blank space in the two spaces I allowed for them, and 
  137. started accepting information on the first space of the underlines.  Following 
  138. the end of the INPUT statement, I moved back and 'erased' the question mark by 
  139. printing a blank space over it.  This has the effect of making it look like 
  140. the question mark INPUT provides is moving from each 'input field' to the next 
  141. one, and the operator can see where he/she is to put more information by 
  142. looking for the question mark.
  143.  
  144. Using this series of steps, the next five steps in the flowchart from GWBT02 
  145. are as follows:
  146.  
  147. LINES 300 to 330 accomplish step four:  Move the cursor to the check number 
  148. area and accept the check number.
  149.  
  150. LINES 340 to 370 accomplish step five:  Move the cursor to the check date area 
  151. and accept the check date.
  152.  
  153. LINES 380 to 410 accomplish step six:  Move the cursor to the payor line and 
  154. accept the payor's name.
  155.  
  156. LINES 420 to 450 accomplish step seven:  Move the cursor to the amount line 
  157. and accept the amount of the check.
  158.  
  159. LINES 460 to 490 accomplish step eight:  Move the cursor to the memo line and 
  160. accept the check memorandum (information).
  161.  
  162. LINE 500 is the BASIC 'END' command that tells BASIC we're done with our 
  163. program, and LINE 510 is our remark line that tells us we're finished as well.
  164.  
  165. As I stated previously, THIS IS ONLY ONE WAY that the necessary steps can be 
  166. carried out!  If yours is different, the only thing you really need to be 
  167. concerned about is that it accomplishes the job properly!
  168.  
  169. (A SHORT SIDELINE ON ETHICS OF PROGRAMMING:)
  170.  
  171. It never hurts to look at how others solve programming problems.  Everyone 
  172. looks at a problem a little bit differently, and someone else's approach might 
  173. appeal to you more than your own.  However, you want to use caution in 
  174. 'borrowing' parts of other people's programs.  Rich Little once quoted that 
  175. "Imitation is the sincerest form of flattery"; but in the computer world the 
  176. saying should probably be more like, "Imitation is the basis for litigation"!
  177.  
  178. The materials presented in these GWBT files are placed in the 'public domain' 
  179. by my own actions.  That is, I freely allow others to use them, as long as I 
  180. am given credit (see the new section at the end, the DISCLAIMER), and many 
  181. others do similarly release some of their work into the 'public domain' as 
  182. well.
  183.  
  184. However, COPYRIGHTED materials are a different matter.  Depending on the mood 
  185. of the judge in the week in question, copyright by the author can extend all 
  186. the way from the actual way the program was written to the way it 'looks and 
  187. feels', how it operates and appears on the screen!  'Borrowing' ideas from 
  188. copyrighted material is not wise, as the copyright holder then may have legal 
  189. basis to sue you!
  190.  
  191. My utilities (RLEPRT, an RLE graphic display program; and TTIME, a file 
  192. transfer time estimation utility) are copyrighted material, and I assure you 
  193. that I would NOT be very happy to see my months of work copied by someone else 
  194. in disregard of my copyright.  I can also assure you that any other author of 
  195. software would respond in the same manner!
  196.  
  197. Some authors do give out copies of their 'source code' (computereese for the 
  198. program that does the work) as part of a package.  It is made quite clear that 
  199. you are allowed only limited rights to use this 'source code' for your own 
  200. personal use, and that this does NOT include packaging parts of it in your own 
  201. software.
  202.  
  203. I would suggest that as you look at others programs, and see parts of them 
  204. that you like, you should contact the author BEFORE using any part of his 
  205. programs within your own!  This is the best way to avoid future problems.
  206.  
  207. Enough preaching for now!  Let's dig into this month's tutorial topic:
  208.  
  209. *************************************************************************
  210. GW-BASIC TUTORIAL PART THREE:  Using disk and printer from BASIC
  211. *************************************************************************
  212.  
  213. So far, all our BASIC efforts have been centered around using the keyboard and 
  214. screen for all input and output.  This is fine for programs that are run, then 
  215. the information that is either given to the program or generated by the 
  216. program are never needed again.  Suppose we want to keep some record of what 
  217. we've done, either in printed form, or in a form that we can use again?
  218.  
  219. It is possible, however, to use other devices that are connected to your 
  220. computer (that is, the disk and printer) to make such permanent copies of our 
  221. information.
  222.  
  223. As an illustration, think for a moment about the old-time switchboard 
  224. operators, circa 1930.  If you had an incomming call, the operator would 
  225. connect it to your telephone by plugging a cable into a jack labeled with your 
  226. name or phone number, and if you wanted to make an outgoing call, the operator 
  227. would plug into your jack one of the outgoing lines.
  228.  
  229. Now, imagine that our operator has six lines available to use, but three of 
  230. them are reserved.  One is marked for normal incomming calls, one is marked 
  231. for normal outgoing calls, and one is marked for emergency calls only.
  232.  
  233. This leaves the operator three lines that can be connected as necessary.  
  234. These lines can used to communicate with any telephone connected to the 
  235. operator's switchboard, and depending on the type of phone, can be for 
  236. incomming calls only, outgoing calls only, or for both incomming and outgoing 
  237. calls.
  238.  
  239. BASIC's input and output function much the same as this illustration.  BASIC 
  240. normally reserves six input/output 'handles' for itself, and assigns three of 
  241. them for special functions.  The first is the 'standard input' (normally your 
  242. keyboard), the second is the 'standard output' (normally your monitor), and 
  243. the third is the 'error output' (always your monitor).  This leaves you three 
  244. channels (referred to in computereese as 'handles') for you to use.
  245.  
  246. To assign one of the three available 'handles' to another input or output 
  247. device, you need to tell BASIC which device you'll be using, and in what mode 
  248. you'll be using it.  Each device that BASIC can communicate with has a name 
  249. that describes it to both BASIC and the user.
  250.  
  251. Please don't become confused about the word 'device'.  I (and BASIC) use the 
  252. word device to refer to any physical item (parallel printers, disk files, 
  253. modems, serial port printers, and so on) that BASIC can communicate with or 
  254. exchange information to or from.  Just think that in any place in the 
  255. following section that you see the word 'device', you can replace it with the 
  256. desired input/output device, such as replacing device with 'printer', 'modem', 
  257. 'serial port', 'disk file' and so on.
  258.  
  259. BASIC understands the following devices and can use them as listed:
  260.  
  261. Device Name             What device is and how you can use it:
  262.  
  263.   KYBD:                 Keyboard (Input only)
  264.   SCRN:                 Screen (Output only)
  265.   LPT1:                 Parallel Printer Port #1 (Output only)
  266.   LPT2:                 Parallel Printer Port #2 (Output only)
  267.   LPT3:                 Parallel Printer Port #3 (Output only)
  268.   COM1:                 Serial Port #1 (Input or Output)
  269.   COM2:                 Serial Port #2 (Input or Output)
  270.  [filename]             Disk file (Input or Output)
  271.  
  272. Note that with the exception of the last device ([filename]), all device names 
  273. have four letters and have a colon (:) at the end of their name.
  274.  
  275. The use of [filename] as a device is a special case.  In this case, the 
  276. [filename] is any allowable DOS file name, and may need to include information 
  277. on what drive or directory the file is in.
  278.  
  279. The way you tell BASIC you want to use an alternate input/output device is 
  280. with the OPEN command:
  281.  
  282.         OPEN "[device]" FOR [access] AS #[number]
  283.  
  284. where:
  285.  
  286.         [device] is either one of the device names listed above, or a valid 
  287.         DOS filename (detailed below)
  288.  
  289.         [access] is either INPUT or OUTPUT (we will cover other modes later
  290.         but for now these two will do what we need)
  291.  
  292.         [number] is a number from 1 to 3
  293.  
  294. Let's look at some examples of opening input and output:
  295.  
  296. To use a file called PROGRAM.LST in the PROGRAM subdirectory of the C: drive, 
  297. and use this file for input as your first device:
  298.  
  299.         OPEN "C:\PROGRAM\PROGRAM.LST" FOR INPUT AS #1
  300.  
  301. To open your printer (attached to the first port) for printing as the second
  302. device:
  303.  
  304.         OPEN "LPT1:" FOR OUTPUT AS #2
  305.  
  306. You can only use each available 'handle' (device number) once.  For example, 
  307. you cannot open a file for input and a printer as:
  308.  
  309.         OPEN "C:\PROGRAM\PROGRAM.LST" FOR INPUT AS #1
  310.         OPEN "LPT1:" FOR OUTPUT AS #1
  311.  
  312. because BASIC would then become confused as to which #1 you meant - was it the 
  313. printer, or the disk file?  Each printer, file, serial port, and so on that 
  314. you give a 'handle' (number) to must have a different number, between 1 and 3.
  315.  
  316. If you wish to re-use a 'handle' for another device, you must first close it.  
  317. BASIC does this by using the CLOSE command:
  318.  
  319.         CLOSE #[number]
  320.  
  321. where [number] is the 'handle' to close.
  322.  
  323. Once you have opened a device, you then use modified forms of the INPUT and 
  324. PRINT statements to either input (get information from the device to BASIC) or 
  325. output (send information from BASIC to the device) as shown below:
  326.  
  327.         INPUT #[number],[variable]
  328.  
  329. where [number] is the device number, and [variable] is the information that 
  330. you want to read in from that device; and
  331.  
  332.         PRINT #[number],[variable]
  333.  
  334.                                     - or -
  335.  
  336.         PRINT #[number],"[string to be printed]"
  337.  
  338. where [number] is the device number, and either [variable] is the variable to 
  339. print, or [string to be printed] is the information you want to print to that 
  340. device.
  341.  
  342. Let's look at a sample program to show how we can use these alternate input 
  343. and output devices:
  344.  
  345. <please refer to enclosed file: INANDOUT.BAS>
  346.  
  347. To make this program work, I needed to sneak in a few things we haven't 
  348. covered yet, so let me briefly explain them:
  349.  
  350. In lines 280 and 390, you see:  IF LINEOFDATA$="" THEN GOTO (line number).  
  351. This is simply a test where if the variable LINEOFDATA$ is empty (for string 
  352. variables, an empty variable is represented by the two quote characters next 
  353. to each other ("") with nothing between.  If the variable LINEOFDATA$ is 
  354. indeed empty, then the GOTO command is executed.  If LINEOFDATA$ is NOT empty, 
  355. then BASIC continues with the next program line as numbered.
  356.  
  357. The GOTO command used in the above lines, and also in lines 300 and 410 are 
  358. simply commands that tell BASIC to disregard the normal rule to start at the 
  359. lowest numbered line and keep running each line, in order, until the END.  A 
  360. GOTO tells BASIC to go to the line number listed after the word GOTO.
  361.  
  362. In summary, the basic points to remember about opening devices for BASIC to 
  363. read from or write to are as follows: 
  364.  
  365. 1)  The device must either be a disk file, or one of the devices listed in the 
  366. list of devices at the beginning of this section of the tutorial
  367.  
  368. 2)  Each device, when opened, must have a unique number (either 1, 2 or 3) 
  369. assigned to it to identify it to both you and BASIC
  370.  
  371. 3)  Once opened, you use modified versions of INPUT/LINE INPUT and PRINT to 
  372. either read from the device (this is INPUT or LINE INPUT) or to send 
  373. information to the device (this is either PRINT or PRINT USING)
  374.  
  375. 4)  When finished with a device, you should CLOSE the device so that you can 
  376. either re-use the device number ('handle') or to be sure that all information 
  377. written to the file is written out.
  378.  
  379. ***************************************************************************
  380.      HOMEWORK ASSIGNMENT #2 - Using DISK files to save information
  381. ***************************************************************************
  382.  
  383. You have delivered your check 'input screen' to the customer, and he is 
  384. extremely happy with it!  He has now requested that you see if there is a 
  385. way to save information entered on each check to disk.
  386.  
  387. This is the list of information that he wants saved to disk:
  388.  
  389.         Check Number                    Six characters
  390.         Check Date                      Ten characters (00/00/0000)
  391.         Paid To                         Fifty characters
  392.         Amount                          Ten characters (0000000.00)
  393.         Memo                            Forty characters
  394.  
  395. This information should be saved to a disk file called CHECKS.DAT, and should 
  396. be saved in a format that can be easily read as needed.
  397.  
  398. Since BASIC (and DOS) work best with files where all records (each check we 
  399. enter will be a 1-line 'record') are multiples of 2, let's add eleven blank 
  400. spaces to the end of the check data.  Remember also that BASIC will add a 
  401. carriage return (ENTER) to the end of each line, so our layout for the 
  402. CHECKS.DAT file looks like this:
  403.  
  404.         Check Number                    Six characters (000000)
  405.         Check Date                      Ten spaces, character-based
  406.         Paid To                         Fifty spaces, character-based
  407.         Amount                          Ten characters (0000000.00)
  408.         Memo                            Forty spaces, character-based
  409.         Filler                          Eleven characters, blank
  410.         Carriage Return                 One character, supplied by BASIC
  411.  
  412. The total length of each 'check record' is now 128 characters, and this will 
  413. allow BASIC and DOS to use the disk space in the most efficient, fastest 
  414. manner possible.
  415.  
  416. The customer also wants to be able to press the ENTER key, without entering a 
  417. check number, to show that there are no more checks to enter.  This would have 
  418. the effect of making the check number equal to zero.
  419.  
  420. Here's a flowchart of what we need to accomplish this job:
  421.  
  422.  
  423.         ---------->------- Ask user for check number
  424.         |                              |
  425.         |                              |
  426.         |               Is check number equal to zero?
  427.         |                              |
  428.         |                              |              YES
  429.         |                              |-----------------------------
  430.         ^                             N|                             |
  431.         |                             O|                      Close Disk Files
  432.         |                              |                          and END
  433.         ^                   Ask user for check data
  434.         |                              |
  435.         |                     Ask user for Payor
  436.         |                              |
  437.         ^                Ask user for amount of check
  438.         |                              |
  439.         |                  Ask user for memorandum
  440.         |                              |
  441.         |                 Print information to disk
  442.         |                              |
  443.         -<----------<------------------
  444.  
  445. I want you to construct a PRINT USING template that will print the information 
  446. to disk in the order listed above, so your template will need to contain the 
  447. following 'fields' of the listed lengths, in this order:
  448.  
  449. 6 numeric, 10 alphabetic, 50 alphabetic, 10 numeric (seven numbers, decimal 
  450. point, 2 numbers), 40 alphabetic, 12 alphabetic.
  451.  
  452. Review the 'PRINT USING' information for how to set this up.  If you cannot 
  453. figure it out, ask for help in the IBMNEW/Basic Workshop message area for 
  454. help.  I would like you to try it on your own first.
  455.  
  456. You should be able to re-use all of your earlier homework assignment from 
  457. GWBT02, with these additions:
  458.  
  459. 1)  Add the test to see if the check number is zero.  If it is, use a GOTO to 
  460. take the program to the end where you will CLOSE your disk file and END
  461.  
  462. 2)  Add the lines necessary to use PRINT USING to print the check information 
  463. to disk, then a GOTO to go back and get another check entry from the user.
  464.  
  465. 3)  If you want to add (a) line(s) to a BASIC program, all you need to do is 
  466. to see where you want to add it, then use a number between the two lines for 
  467. the insertion.  For example, if you want to insert a line after 140 and before 
  468. 150, just type it in, using line number 145.  LIST your program, and behold!  
  469. It's there and in the right order.  If you want to add more than one, use a 
  470. smaller number (in the example, we could add lines 141,142,143,...,147,148,149 
  471. adding nine lines in all).
  472.  
  473. This is where life begins to get a little harder!  Remember, if you have any 
  474. questions, or get absolutely stumped, just GO IBMNEW and enter Message Area 
  475. #10 (BASIC Workshop) and we'll get you pointed in the right direction!
  476.  
  477. Good Luck and Good Programming!
  478. <end of file>
  479.  
  480.